package com.config;

import com.bean.other.ResponseData;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;

@Slf4j
@Order(1)
//@Component
@WebFilter(urlPatterns = "/api/*")
public class LoginFilter implements Filter {

    @Autowired
    private JWTUtil jwtUtil;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // 初始化操作
        System.out.println("初始化LoginFilter");
    }

    private final ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {



        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse =(HttpServletResponse) response;

        //httpRequest.getRequestDispatcher("/toLoginError?error="+"token无效").forward(httpRequest,httpResponse);


        log.warn("经过LoginFilter这个过滤器");
        log.warn("请求的路径为："+ httpRequest.getRequestURL().toString());

        String token=httpRequest.getHeader("token");
        String tokenUserId=httpRequest.getHeader("tokenUserId");
        if(token!=null&tokenUserId!=null){
            log.info("token:"+token);
            //判断token是否有效
            String message=jwtUtil.validateToken(token,tokenUserId);
            if(message.equals("token有效")){
                String newToken=jwtUtil.refreshToken(token);
                if(newToken!=null){
                    httpResponse.setHeader("token",newToken);
                }
                chain.doFilter(httpRequest,httpResponse);
            }else{
                httpRequest.getRequestDispatcher("/toLoginError?error="+message).forward(httpRequest,httpResponse);
            }
            //判断token是否过期
       }else{
            log.info("未登录:"+token);
            httpRequest.getRequestDispatcher("/toLoginError?error="+"未登录").forward(httpRequest,httpResponse);
        }




        // 获取 User-Agent 来判断设备类型
     /*   String userAgent = httpRequest.getHeader("User-Agent");
        log.info(userAgent);
        boolean isMobile = userAgent != null && (userAgent.contains("iPhone") || userAgent.contains("Android"));

        if (isMobile) {
            // 如果是移动端请求，返回移动端的 index.html
            httpRequest.getRequestDispatcher("index_uniapp.html").forward(request, response);
        } else {
            // 否则返回PC端的 index.html
            httpRequest.getRequestDispatcher("index_web.html").forward(request, response);
        }*/
    }

    @Override
    public void destroy() {
        // 销毁操作
    }
}